package luanluan.data.resource 
{
	import flash.display.Loader;
	import flash.events.Event;
	import luanluan.events.EventCenter;
	import luanluan.events.ResEvent;
	
	/**
	 * 资源加载管理
	 * 加载swf文件并将数据写入应用程序域
	 * 以便使用swf库中的相关资源
	 * @author luanluan
	 */
	public class ResManager 
	{
		//	*是否正在加载
		private var _isLoad:Boolean = false;
		
		//	*索要加载的数据
		private var _data:Vector.<ResLoadInfo>;
		
		//	*正在加载第几个文件
		private var _index:int = 0;
		
		/**
		 * 加载资源至域,加载完成后
		 * @param	urls	所有需要加载的文件名
		 */
		public function loadRes(...urls):void
		{
			if (_isLoad)
			{
				trace("正在加载资源中...");
				return;
			}
			
			if (urls.length > 0)
			{
				var len:int = urls.length;
				_data = new Vector.<ResLoadInfo>(len , true);
				for (var i:int = 0; i < len; i++) 
				{
					var temp:ResLoadInfo = new ResLoadInfo(urls[i]);
					_data[i] = temp;
				}
				_isLoad = true;
				_index = 0;
				loadNext();
			}
		}
		
		/**
		 * 加载下一个
		 */
		private function loadNext():void 
		{
			if (_index >= _data.length)
			{
				complete();
				return;
			}
			
			var temp:ResLoadInfo = _data[_index];
			temp.completeCallBack = loadNext;
			temp.start();
			_index++;
		}
		
		/**
		 * 加载完成
		 */
		private function complete():void
		{
			EventCenter.dispatchEvent(new ResEvent(ResEvent.LOAD_COMPLETE));
			_isLoad = false;
		}
		
		public function ResManager(_single:Single) 
		{
			if(!_single)
			{
				throw new Error("请用静态方法获取单例的引用!")
			}
		}
		
		private static var _instance:ResManager = null;
		public static function get instance():ResManager
		{
			if(_instance == null)
				_instance = new ResManager(new Single());
			return _instance;
		}
		
	}
	
}
class Single{}